<?php
require_once('commons.php');

switch ($_REQUEST['op']) {
	case 'logIn':
		$user = $_POST['user'];
		$pass = hashPass($_POST['pass']);
		
		// USER AUTHENTICATION PROCESS
		$query = "SELECT users.user, users.type, users.name, users.surname FROM users 
			WHERE users.user='$user' AND users.pass='$pass'";
		$result = executeQuery($query);
		$userData = false;
		if(mysqli_num_rows($result)>0) {
			$userData = mysqli_fetch_assoc($result);
		}
		mysqli_free_result($result);

		$url = '../index.php';
		$msg = 'Error de autenticación de usuario';
		
		// CREATE SESSION PROCESS
		if($userData) {
			session_start();
			$_SESSION['e22456391ee147ba0c4426e1f77f8936cc24b452'] = 'f9a899183d84ba00f9aa3220c16098c0bc4f365e';
			$_SESSION['user'] = $userData['user'];
			$_SESSION['type'] = $userData['type'];
			$_SESSION['name'] = $userData['name'];
			$_SESSION['surname'] = $userData['surname'];
			$url = '../start.php';
			$msg = 'Bienvenid@ '.$userData['name'];
		}
		break;
	
	default:
	  session_start();
	  $url = '../index.php';
	  $msg = 'Acceso restringido a usuarios autenticados';
	  if(isset($_SESSION['e22456391ee147ba0c4426e1f77f8936cc24b452']) )  {
	  	$msg = 'Sesión cerrada con éxito';
		  $_SESSION = array();
			session_destroy();	  	
	  }
	  break; 
}
goToURL($url,$msg);
?>